package com.zxcar.control;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;

import com.allen.library.RxHttpUtils;
import com.allen.library.interceptor.Transformer;
import com.allen.library.observer.CommonObserver;
import com.allen.library.utils.ToastUtils;
import com.zxcar.control.bean.RepData;
import com.zxcar.control.http.ApiService;

import java.net.Socket;

/**
 * 通过HTTP请求来控制，避免占用WiFi，无法联网。
 * 只需要处于同一局域网即可
 */
public class MainRestFulModeActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView mLogLeft;
    Socket socket = null;
    private Button connectCheck;
    Button speedUp;
    private Button speedDown;
    private Button stopCar;
    private SwitchCompat ledControl;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(getResources().getColor(R.color.white));
        }
        getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        setContentView(R.layout.activity_main);

        mLogLeft = (TextView) findViewById(R.id.log_left);
        ledControl = findViewById(R.id.light_led);

        connectCheck = (Button)findViewById(R.id.button_connect);
        speedUp = (Button)findViewById(R.id.button_speed_up);
        speedDown = (Button)findViewById(R.id.button_speed_down);
        stopCar = (Button)findViewById(R.id.button_stop_motor);
        if(connectCheck != null)
            connectCheck.setOnClickListener(this);
        if(speedUp != null) {
            speedUp.setOnClickListener(this);
        }
        if(speedDown != null)
            speedDown.setOnClickListener(this);
        if(stopCar != null)
            stopCar.setOnClickListener(this);


        ledControl.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    RxHttpUtils.createApi("http_local_url_tag", "http://192.168.3.165", ApiService.class)
                            .sendLedMsg("a").compose(Transformer.<RepData>switchSchedulers())
                            .subscribe(new CommonObserver<RepData>() {
                                @Override
                                protected void onError(String errorMsg) {
                                    Log.e("printDataResp","Err : "+errorMsg);
                                }

                                @Override
                                protected void onSuccess(RepData repData) {
                                    if (repData!=null){
                                        Log.e("printDataResp","Success : "+repData.getReturn_value());
                                    }
                                }
                            });
                }else {
                    RxHttpUtils.createApi("http_local_url_tag", "http://192.168.3.165", ApiService.class)
                            .sendLedMsg("b").compose(Transformer.<RepData>switchSchedulers())
                            .subscribe(new CommonObserver<RepData>() {
                                @Override
                                protected void onError(String errorMsg) {
                                    Log.e("printDataResp","Err : "+errorMsg);
                                }

                                @Override
                                protected void onSuccess(RepData repData) {
                                    if (repData!=null){
                                        Log.e("printDataResp","Success : "+repData.getReturn_value());
                                    }
                                }
                            });
                }
            }
        });

        RockerView rockerViewLeft = (RockerView) findViewById(R.id.rockerView_left);
        if (rockerViewLeft != null) {
            rockerViewLeft.setCallBackMode(RockerView.CallBackMode.CALL_BACK_MODE_STATE_CHANGE);
            rockerViewLeft.setOnShakeListener(RockerView.DirectionMode.DIRECTION_4_ROTATE_45, new RockerView.OnShakeListener() {
                @Override
                public void onStart() {
                    mLogLeft.setText("停");
                    Log.e("getPrintLog","yes onStart");
                }

                @Override
                public void direction(RockerView.Direction direction) {
                    String message = "";
                    switch (direction) {
                        case DIRECTION_LEFT:
                            message = "左转";
                            TCPSend(3);
                            Log.e("getPrintLog","yes 左转");
                            break;
                        case DIRECTION_RIGHT:
                            message = "右转";
                            TCPSend(4);
                            Log.e("getPrintLog","yes 右转");
                            break;
                        case DIRECTION_UP:
                            message = "前进";
                            TCPSend(1);
                            Log.e("getPrintLog","yes 前进");
                            break;
                        case DIRECTION_DOWN:
                            message = "后退";
                            TCPSend(2);
                            Log.e("getPrintLog","yes 后退");
                            break;
                        case DIRECTION_CENTER:
                        default:
                            break;
                    }
                    mLogLeft.setText(message);
                }
                @Override
                public void onFinish() {
                    mLogLeft.setText("停");
                    TCPSend(5);
                    Log.e("getPrintLog","yes onFinish");
                }
            });
        }

        RockerView rockerViewRight = (RockerView) findViewById(R.id.rockerView_right);
        if (rockerViewRight != null) {
            rockerViewRight.setCallBackMode(RockerView.CallBackMode.CALL_BACK_MODE_STATE_CHANGE);
            rockerViewRight.setOnShakeListener(RockerView.DirectionMode.DIRECTION_4_ROTATE_45, new RockerView.OnShakeListener() {
                @Override
                public void onStart() {
                }

                @Override
                public void direction(RockerView.Direction direction) {
                    String message = "";
                    switch (direction) {
                        case DIRECTION_LEFT:
                            message = "左转";
                            TCPSend(6);
                            break;
                        case DIRECTION_RIGHT:
                            message = "右转";
                            TCPSend(7);
                            break;
                        case DIRECTION_UP:
                            message = "抬头";
                            TCPSend(8);
                            break;
                        case DIRECTION_DOWN:
                            message = "低头";
                            TCPSend(9);
                            break;
                        case DIRECTION_CENTER:
                            break;
                        default:
                            break;
                    }
                }

                @Override
                public void onFinish() {
                    TCPSend(10);
                }
            });
        }
    }

    private void Connect() {
        RxHttpUtils.createApi("http_local_url_tag", "http://192.168.3.165", ApiService.class)
                .sendMsg(66+"").compose(Transformer.<RepData>switchSchedulers())
                .subscribe(new CommonObserver<RepData>() {
                    @Override
                    protected void onError(String errorMsg) {
                        Log.e("printDataResp","Err : "+errorMsg);
                    }

                    @Override
                    protected void onSuccess(RepData repData) {
                        if (repData!=null){
                            if (repData.getReturn_value()==66){
                                ToastUtils.showToast("连接成功");
                            }
                        }
                    }
                });
    }

    private void TCPSend(final String data) {
    }
    private void TCPSend(final int data) {

        RxHttpUtils.createApi("http_local_url_tag", "http://192.168.3.165", ApiService.class)
                .sendMsg(data+"").compose(Transformer.<RepData>switchSchedulers())
                .subscribe(new CommonObserver<RepData>() {
                    @Override
                    protected void onError(String errorMsg) {
                        Log.e("printDataResp","Err : "+errorMsg);
                    }

                    @Override
                    protected void onSuccess(RepData repData) {
                        if (repData!=null){
                            Log.e("printDataResp","Success : "+repData.getReturn_value());
                        }
                    }
                });

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_connect:
                Connect();
                break;
            case R.id.button_speed_up:
                TCPSend(12);
                break;
            case R.id.button_speed_down:
                TCPSend(22);
                break;
            case R.id.button_stop_motor:
                TCPSend(5);
                break;
            default:
                break;
        }
    }
}